<?php
namespace TrainManage\Controller;

use Think\Controller;
use Think\Model;

/*class Test
{
	//public $auth;
	public $result;
}*/
class Test
{
	public $code;
	public $msg;
	public $count;
	public $data;
}
class TrainController extends Controller
{
	/*public function index2()
	{
		$this->display();
	}*/
	public function showlist()
	{
		/*C('SHOW_PAGE_TRACE', true);
		$tra = M('train');
		{
			$info = $tra->where('status=1')->order('no desc')->select();
			$this->assign('info', $info);
			
		}*/
		if(session('status')>0)
		{
			$this->display();
		}
		else
		{
			$this->error("权限不足");
		}
	}
	/*public function tianjia1()
	{
		
			//C('SHOW_PAGE_TRACE', true);
		$tra=M('train');
			//dump($_POST);
			//$test=new Test();
			//$tra = D('train');//用D方法调用create实现自动验证数据库和模板名一致,实例化模板的同时操作数据库
		if (!empty($_POST))//$_POST不会为空，submit有值,但删除后自动校验直接报错
		{
			if (session('status') > 1)
			{
				$upload = new \Think\Upload();//规格设置
				$upload->maxSize = 0;
				$upload->rootPath = './Public/Train/';
				$upload->autoSub = false;
				$info = $upload->upload();
				foreach ($info as $file) 
				{//遍历information,file与前段表单名称无关
					$savename = $file['savename'];
					$initname = $file['name'];
				}
        		$tra->id=$z->field('id');
        		$tra->filename=$file['savename'];

        		$tra->save();
        		$tra->title=I('title');
				$tra->participant=I('participant');
				$tra->content=I('content');
				$tra->date=I('date');
        		$tra->no=I('no');
        		$tra->host=session('id');
        		$tra->type=I('type');
				$tra->filename = $initname;//表单中无文件名数据手动添加
				$tra->savename = $savename;
				$z = $tra->add();
				//dump($z);
				//dump($tra->type);
				if ($z) 
				{
					$result='添加成功';
				}
				else
				{
					$result='添加失败';
				}
				
			}
			else
			{
				$result='权限不足';
			}
			$this->ajaxReturn($result);
		}			
		else
		{
			$result="4";
		}
		$this->display();
		//dump($result);
		
	}*/
	public function tianjia()
	{
		$tra=M('train');
       	if (session('status') > 1)
            {
            	if(empty($_POST))
            	$this->display();
            }
          else
          {
          	$this->error('权限不足','showlist.html');
          }
	}
	public function xiugai($id)
	{
		$tra = M('train');
		$result=$tra->field('id,host')->where("id='$id'")->find();
		/*echo session('id');
		echo "<br>";
		echo $result['host'];
		echo "<br>";
		echo $id;*/
		if ((session('status') == 4 )|| (session('id') == $result['host']) ){//result数组
			$this->assign('info2',$result);
			$this->display();
		}
		else
		{
			$this->ajaxReturn(1);
			// $this->error('权限不足','showlist.html');
		}
	}
	public function xiugai1($id)//文件
	{

		$tra = D('train');
			if (!empty($_POST)) {
				$shuju = $tra->create();
				if ($shuju)
				{
					$tra->id = $id;
					$tra->savename=I('savename');
					$tra->filename=I('initname');
					$y = $tra->save();
					if($y)
					{
						$result='修改成功';
					}
					else
					{
						$result='修改失败';
					}
				} 
			else
			{
				$result='数据为空';
			}
		}
			$this->ajaxReturn($result);
	}
	public function showmore($id)
	{
		//echo $id;
		$tra = M('train');
		$info1 = $tra->where("id='$id'")->find();
		$this->assign('info1',$info1);
		$this->display();
	}
	public function showmore1($id)//后台给id赋值在数据库进行搜索
	{
		//C('SHOW_PAGE_TRACE', true);
		
		$tra = M('train');
		$info1 = $tra->join('join sz_user on sz_train.host=sz_user.id')->field('sz_train.id,name,no,title,content,participant,type,date')->where("sz_train.id='$id'")->find();
		//$info1=$tra->find($id);//find()取一维数组//双引号解析字符串
		//dump($info1);
		//$this->display();
		$this->ajaxReturn($info1);
	}
	public function delete($id)
	{
		$tra = M('train');
		$result=$tra->field('id,host')->where("id=$id")->find();
		if ((session('status') == 4 )|| (session('id') == $result['host']) ){
			$tra->id = $id;//$tra->find($id);
			$tra->status = 0;
			$z = $tra->save();
			if ($z) {
				$result="1";
			} else {
				$result="删除失败";
			}
		}
		else
		{
			$result="权限不足";
		}
			$this->ajaxReturn($result);
	}
	public function getmsg($page,$limit)
	{
		$test=new Test();
		$tra=M('train');
		$start=($page-1)*$limit;
		$info=$tra->join('join sz_user on sz_train.host=sz_user.id')->field('sz_train.id,name,no,title,content,participant,type,date')->where('sz_train.status=1')->order('no desc')->limit($start,$limit)->select();
		$count=$tra->where('status=1')->count();
		$test->code=0;
		$test->msg="";
		$test->count=$count;
		$test->data=$info;
		$this->ajaxreturn($test);
	}
	public function upfile()
	{
		$upload = new \Think\Upload();//规格设置
        $upload->maxSize = 0;
        $upload->rootPath = './Public/Train/';
        $upload->autoSub = false;
        $info = $upload->upload();
        foreach ($info as $file) 
        {//遍历information,file与前段表单名称无关
            $savename = $file['savename'];
            $initname = $file['name'];
        }
            $tra[0]= $initname;//表单中无文件名数据手动添加
            $tra[1]= $savename;
        $this->ajaxReturn($tra);
	}
	public function tianjia1()
    {            
    	//C('SHOW_PAGE_TRACE', true);
        $tra=M('train');
            //dump($_POST);
            //$test=new Test();
            //$tra = D('train');//用D方法调用create实现自动验证数据库和模板名一致,实例化模板的同时操作数据库
        $tra->title=I('title');
        $tra->participant=I('participant');
        $tra->content=I('content');
        $tra->date=I('date');
        $tra->no=I('no');
        $tra->host=session('id');
        $tra->type=I('type');
        $tra->filename =I('initname');//表单中无文件名数据手动添加
        $tra->savename=I('savename');
   		$z = $tra->add();
        //dump($z);
        //dump($tra->type);
        if ($z) 
        {
            $result='添加成功';
        }
        else
        {
            $result='添加失败';//鸡肋
        }
         $this->ajaxReturn($result);
        
        //dump($result);
    }
}
?>